package blue.mqtt4j.internal.client.net;

import blue.mqtt4j.internal.client.handler.MessageHandlerFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.mqtt.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Jin Zheng
 * @since 1.0 2020-10-16
 */
public class MqttMessageHandler extends SimpleChannelInboundHandler<MqttMessage>
{
	private static Logger logger = LoggerFactory.getLogger(MqttMessageHandler.class);

	private final MessageHandlerFactory factory;

	public MqttMessageHandler(MessageHandlerFactory factory)
	{
		this.factory = factory;
	}

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, MqttMessage msg) throws Exception
	{
		logger.debug("Receive: {}", msg);
		factory.handle(msg, ctx.channel());
	}

}
